import 'package:get/get.dart';
import 'package:hygf/http/file_transfer/down_cap.dart';
import 'package:hygf/util/location/location_gaode.dart';

import '../../../../bean/CustomerList.dart';
import '../../../../config/ApiConfig.dart';
import '../../../../config/GlobalConfig.dart';
import '../../../../http/DioClient.dart';
import '../../../../http/core/ResponseData.dart';
import '../../../../util/HDNavigator.dart';
import '../../../../util/LoadingUtils.dart';
import '../../../../view/image_picker_widget.dart';
import '../../exploration/exploration_task/logic.dart';
import '../CustomerDetailPage.dart';
import 'state.dart';

class ExploreMsgLogic extends GetxController {
  final ExploreMsgState state = ExploreMsgState();

  Future<void> submit(int flag) async {
    if (state.rnInfo.id != null) {
      state.addTaKanBean.id = state.rnInfo.id;
    }
    state.addTaKanBean.customerInfoId = state.rnInfo.customerInfoId;
    state.addTaKanBean.userName =
        state.rnInfo.userName ?? GlobalConfig.loginEntity?.userName;
    state.addTaKanBean.phoneNumber =
        state.rnInfo.phoneNumber ?? GlobalConfig.loginEntity?.phoneNumber;
    state.addTaKanBean.surveyMethod = state.rnTypeController.text;
    state.addTaKanBean.surveyDate = state.rnInfo.surveyDate;
    state.addTaKanBean.address = state.rnLocationController.text;
    state.addTaKanBean.addressLongitude =
        state.rnInfo.addressLongitude.toString();
    state.addTaKanBean.addressLatitude =
        state.rnInfo.addressLatitude.toString();
    state.addTaKanBean.remarks = state.rnRemarkController.text;
    state.addTaKanBean.areaName = state.areaNameController.text;
    state.addTaKanBean.areaCapacity = state.areaCapacityController.text;
    state.addTaKanBean.areaDistance = state.areaDistanceController.text;
    if (state.roughSketchController.data.isNotEmpty) {
      if ((state.addTaKanBean.roughSketch ?? []).isEmpty) {
        state.addTaKanBean.roughSketch = [];
      }
      for (var item in state.roughSketchController.data) {
        if (!item.isDel) {
          state.addTaKanBean.roughSketch!.add(item.id ?? 0);
        }
      }
    }
    if (state.surveySheetController.data.isNotEmpty) {
      if ((state.addTaKanBean.surveySheet ?? []).isEmpty) {
        state.addTaKanBean.surveySheet = [];
      }
      for (var item in state.surveySheetController.data) {
        if (!item.isDel) {
          state.addTaKanBean.surveySheet!.add(item.id ?? 0);
        }
      }
    }
    if (state.panoramicPhotoController.data.isNotEmpty) {
      if ((state.addTaKanBean.panoramicPhoto ?? []).isEmpty) {
        state.addTaKanBean.panoramicPhoto = [];
      }
      for (var item in state.panoramicPhotoController.data) {
        if (!item.isDel) {
          state.addTaKanBean.panoramicPhoto!.add(item.id ?? 0);
        }
      }
    }
    if (state.rnGatePhotoController.data.isNotEmpty) {
      if ((state.addTaKanBean.gatePhoto ?? []).isEmpty) {
        state.addTaKanBean.gatePhoto = [];
      }
      for (var item in state.rnGatePhotoController.data) {
        if (!item.isDel) {
          state.addTaKanBean.gatePhoto!.add(item.id ?? 0);
        }
      }
    }
    if (state.oppositeAnglesPhotoController.data.isNotEmpty) {
      if ((state.addTaKanBean.oppositeAnglesPhoto ?? []).isEmpty) {
        state.addTaKanBean.oppositeAnglesPhoto = [];
      }
      for (var item in state.oppositeAnglesPhotoController.data) {
        if (!item.isDel) {
          state.addTaKanBean.oppositeAnglesPhoto!.add(item.id ?? 0);
        }
      }
    }
    if (state.externalDetailPhotoController.data.isNotEmpty) {
      if ((state.addTaKanBean.externalDetailPhoto ?? []).isEmpty) {
        state.addTaKanBean.externalDetailPhoto = [];
      }
      for (var item in state.externalDetailPhotoController.data) {
        if (!item.isDel) {
          state.addTaKanBean.externalDetailPhoto!.add(item.id ?? 0);
        }
      }
    }
    if (state.rnOtherImgController.data.isNotEmpty) {
      if ((state.addTaKanBean.other ?? []).isEmpty) {
        state.addTaKanBean.other = [];
      }
      for (var item in state.rnOtherImgController.data) {
        if (!item.isDel) {
          state.addTaKanBean.other!.add(item.id ?? 0);
        }
      }
    }
    if (check(flag)) {
      state.addTaKanBean.saveOrSubmit = flag;
      ResponseData response = await DioClient()
          .doPost(ApiConfig.reconnaissance, data: state.addTaKanBean);
      if (response.isok) {
        if (response.code == "0") {
          LoadingUtils.showToast(response.msg ?? "上报成功");
          Get.put(ExplorationTaskLogic()).callRefresh();
          HDNavigator.close(result: "refresh");
        } else {
          LoadingUtils.showToast(response.msg ?? "上报失败");
        }
      } else {
        LoadingUtils.showToast(response.msg ?? "上报失败");
      }
    }
  }

  @override
  void dispose() {
    LocationGaode().dispose();
  }

  bool check(int flag) {
    if (flag == 0) {
      return true;
    }
    if ((state.addTaKanBean.userName ?? "").isEmpty) {
      LoadingUtils.showToast("请输入踏勘人员姓名");
      return false;
    }
    if ((state.addTaKanBean.phoneNumber ?? "").isEmpty) {
      LoadingUtils.showToast("请输入踏勘人员电话");
      return false;
    }
    if ((state.addTaKanBean.surveyMethod ?? "").isEmpty) {
      LoadingUtils.showToast("请输入踏勘方式");
      return false;
    }
    if ((state.addTaKanBean.address ?? "").isEmpty) {
      LoadingUtils.showToast("请输入踏勘位置");
      return false;
    }
    if ((state.addTaKanBean.surveyDate ?? "").isEmpty) {
      LoadingUtils.showToast("请输入踏勘时间");
      return false;
    }
    // if ((state.addTaKanBean.areaName ?? "").isEmpty) {
    //   LoadingUtils.showToast("请输入台区名称");
    //   return false;
    // }
    // if ((state.addTaKanBean.areaCapacity ?? "").isEmpty) {
    //   LoadingUtils.showToast("请输入台区容量");
    //   return false;
    // }
    // if ((state.addTaKanBean.areaDistance ?? "").isEmpty) {
    //   LoadingUtils.showToast("请输入台区距离");
    //   return false;
    // }
    if ((state.addTaKanBean.surveySheet ?? []).isEmpty) {
      LoadingUtils.showToast("请上传勘测单");
      return false;
    }
    if ((state.addTaKanBean.roughSketch ?? []).isEmpty) {
      LoadingUtils.showToast("请上传草图");
      return false;
    }
    if ((state.addTaKanBean.panoramicPhoto ?? []).isEmpty) {
      LoadingUtils.showToast("请上传俯视全景图");
      return false;
    }
    if ((state.addTaKanBean.gatePhoto ?? []).isEmpty) {
      LoadingUtils.showToast("请上传建筑正面全景图");
      return false;
    }
    if ((state.addTaKanBean.oppositeAnglesPhoto ?? []).isEmpty) {
      LoadingUtils.showToast("请上传内部细节照片");
      return false;
    }
    if ((state.addTaKanBean.externalDetailPhoto ?? []).isEmpty) {
      LoadingUtils.showToast("请上传外部细节照片");
      return false;
    }
    return true;
  }

  Future<void> initImgUrl() async {
    if ((state.rnInfo.roughSketch ?? "").isNotEmpty) {
      state.roughSketchController.resetImages([]);
      List<ImagePickerData> houseData =
          await DioClient().requestImgUrl(state.rnInfo.roughSketch);
      state.roughSketchController.addImages(houseData);
    }
    if ((state.rnInfo.surveySheet ?? "").isNotEmpty) {
      state.surveySheetController.resetImages([]);
      List<ImagePickerData> surveySheetData =
          await DioClient().requestImgUrl(state.rnInfo.surveySheet);
      state.surveySheetController.addImages(surveySheetData);
    }
    if ((state.rnInfo.panoramicPhoto ?? "").isNotEmpty) {
      state.panoramicPhotoController.resetImages([]);
      List<ImagePickerData> gateData =
          await DioClient().requestImgUrl(state.rnInfo.panoramicPhoto);
      state.panoramicPhotoController.addImages(gateData);
    }
    if ((state.rnInfo.gatePhoto ?? "").isNotEmpty) {
      state.rnGatePhotoController.resetImages([]);
      List<ImagePickerData> panoramicData =
          await DioClient().requestImgUrl(state.rnInfo.gatePhoto);
      state.rnGatePhotoController.addImages(panoramicData);
    }
    if ((state.rnInfo.oppositeAnglesPhoto ?? "").isNotEmpty) {
      state.oppositeAnglesPhotoController.resetImages([]);
      List<ImagePickerData> oapData =
          await DioClient().requestImgUrl(state.rnInfo.oppositeAnglesPhoto);
      state.oppositeAnglesPhotoController.addImages(oapData);
    }
    if ((state.rnInfo.externalDetailPhoto ?? "").isNotEmpty) {
      state.externalDetailPhotoController.resetImages([]);
      List<ImagePickerData> edpData =
          await DioClient().requestImgUrl(state.rnInfo.externalDetailPhoto);
      state.externalDetailPhotoController.addImages(edpData);
    }
    if ((state.rnInfo.other ?? "").isNotEmpty) {
      state.rnOtherImgController.resetImages([]);
      List<ImagePickerData> otherData =
          await DioClient().requestImgUrl(state.rnInfo.other);
      state.rnOtherImgController.addImages(otherData);
    }
  }

  void save() {
    submit(0);
  }

  Future<void> setData(ReconnaissanceLink? reconnaissanceLink) async {
    if (reconnaissanceLink != null) {
      state.rnInfo = reconnaissanceLink;
      await initImgUrl();
      Get.find<RefreshTabLogic>(tag: "1").refreshTabBar();
    }
  }
}
